@echo off
REM 删除旧文件和根目录
del "%~dp0\custom_spider.jar"
rd /s/q "%~dp0\Smali_classes"
REM 清理旧的缝合Smali文件
rd /s/q "%~dp0\Smali_classes_extra"

REM 解码DEX文件为Smali文件
REM ava -jar "%~dp0\3rd\baksmali-2.5.2.jar" d "%cd%\app\build\intermediates\dex\release\minifyReleaseWithR8\classes.dex" -o "%~dp0\Smali_classes"
java -jar "%~dp0\3rd\baksmali-2.5.2.jar" d "%~dp0\..\app\build\intermediates\dex\release\minifyReleaseWithR8\classes.dex" -o "%~dp0\Smali_classes"

REM 缝合jar
REM 解码 xiaomi.jar 中的 classes.dex 为 Smali 文件
java -jar "%~dp0\3rd\baksmali-2.5.2.jar" d "%~dp03rd\xiaomi.jar\classes.dex" -o "%~dp0\Smali_classes_extra"
if %ERRORLEVEL% neq 0 (
    echo Error occurred during baksmali execution for xiaomi.jar.
    exit /b 1
)
REM 清理旧的Smali文件
rd /s/q "%~dp0\spider.jar\smali\com\github\catvod\spider"
rd /s/q "%~dp0\spider.jar\smali\com\github\catvod\parser"
rd /s/q "%~dp0\spider.jar\smali\com\github\catvod\js"

REM 创建必要的目录结构
if not exist "%~dp0\spider.jar\smali\com\github\catvod\" md "%~dp0\spider.jar\smali\com\github\catvod\"

REM 移动新的Smali文件
move "%~dp0\Smali_classes\com\github\catvod\spider" "%~dp0\spider.jar\smali\com\github\catvod\"
move "%~dp0\Smali_classes\com\github\catvod\parser" "%~dp0\spider.jar\smali\com\github\catvod\"
move "%~dp0\Smali_classes\com\github\catvod\js" "%~dp0\spider.jar\smali\com\github\catvod\"

REM 缝合jar进行移动SMail文件到Spider中
REM /E：复制子目录，包括空目录。
REM /XO：排除较旧的文件，即跳过目标目录中已存在的文件
for %%i in (spider parser js) do (
    if exist "%~dp0\Smali_classes_extra\com\github\catvod\%%i" (
        REM 确保目标目录存在
        if not exist "%~dp0\spider.jar\smali\com\github\catvod\%%i" md "%~dp0\spider.jar\smali\com\github\catvod\%%i"
        REM 合并文件，跳过已存在的文件
        for /R "%~dp0\Smali_classes_extra\com\github\catvod\%%i" %%f in (*.smali) do (
            if not exist "%~dp0\spider.jar\smali\com\github\catvod\%%i\%%~nxf" (
                copy "%%f" "%~dp0\spider.jar\smali\com\github\catvod\%%i\" >nul
            )
        )
    )
)



REM 重新打包为JAR文件
java -jar "%~dp0\3rd\apktool_2.4.1.jar" b "%~dp0\spider.jar" -c

REM 检查生成的 dex.jar 文件是否存在
if not exist "%~dp0\spider.jar\dist\dex.jar" (
    echo dex.jar not found in spider.jar\dist.
    exit /b 1
)

REM 移动生成的JAR文件
move "%~dp0\spider.jar\dist\dex.jar" "%~dp0\custom_spider.jar"
if %ERRORLEVEL% neq 0 (
    echo Error occurred during moving dex.jar.
    exit /b 1
)

REM 生成MD5校验文件
certUtil -hashfile "%~dp0\custom_spider.jar" MD5 | find /i /v "md5" | find /i /v "certutil" > "%~dp0\custom_spider.jar.md5"

REM 清理临时目录
rd /s/q "%~dp0\spider.jar\build"
rd /s/q "%~dp0\spider.jar\smali"
rd /s/q "%~dp0\spider.jar\dist"
rd /s/q "%~dp0\Smali_classes"
REM 清理旧的缝合Smali文件
rd /s/q "%~dp0\Smali_classes_extra" 2>nul